import logging
import logs
import os
import sys
import uuid
import confhelper
os.chdir(os.path.dirname(__file__))

from asyncio import InvalidStateError
from playwright.sync_api import Playwright, sync_playwright

import dbmapper
from entity import LinkMonResult, MonLinkBatch, MonLinkVersion, MonLink

logging.config.fileConfig("../logging.conf", disable_existing_loggers=False)
logger = logging.getLogger("tb")

wiew_size = {"width": 1080, "height": 3240}

work_dir = confhelper.confdata().get("work_dir")
linkpath_prefix = confhelper.confdata().get("linkpath_prefix")+"tb/"
monLinks = []

logger.info("sys.argv: {}".format(sys.argv))
version = sys.argv[1]

def run(playwright: Playwright, link: MonLink) -> None:
    # browser = playwright.firefox.launch(headless=False,slow_mo=3000)
    # context = browser.new_context(screen=wiew_size, viewport=wiew_size,storage_state="tb_state.json")
    browser = playwright.firefox.launch(headless=False, slow_mo=4000, args=["--start-maximized"])
    context = browser.new_context(no_viewport=True, storage_state="tb_state.json")
    page = context.new_page()

    i = 0
    url = link.url
    linkId = link.id

    currentPage = link.currentPage + 1

    logger.info("开始遍历 {}，linkId:{}".format(url, linkId))
    page.goto(url)
    page.wait_for_timeout(1000)

    passCheck(page)

    if page.locator("text=没有找到相应的店铺信息").count() > 0:
        logger.error("无效店铺: {}".format(url))
        dbmapper.MonLinkMapper.updateInvalid(MonLink(id=link.id))
        return

    if page.locator("text=店铺终止经营公告").count() > 0:
        logger.error("无效店铺: {}".format(url))
        dbmapper.MonLinkMapper.updateInvalid(MonLink(id=link.id))
        return

    passCheck(page)

    if page.locator(".J_SearchForm").locator("text=搜本店").count() > 0 :
        page.locator(".J_SearchForm").locator("text=搜本店").click()
    elif page.locator(".all-cats.popup-container").locator("text=所有分类").count() > 0 :
        page.locator(".all-cats.popup-container").locator("text=所有分类").click()
    elif page.locator("#searchHeader").locator("text=搜本店").count() > 0:
        page.locator('#q').fill('s')
        page.locator("#searchHeader").locator("text=搜本店").click()
        if page.locator(".all-cats.popup-container").locator("text=所有分类").count() > 0:
            page.locator(".all-cats.popup-container").locator("text=所有分类").click()
    elif page.locator("text=全部宝贝").count() > 0 :
        page.locator("text=全部宝贝").click()

    page.wait_for_timeout(1000)
    passCheck(page)

    if page.locator("text=很抱歉，搜索到“0”个宝贝").count() > 0:
        logger.error("店铺无商品: {}".format(url))
        dbmapper.MonLinkMapper.updateInvalid(MonLink(id=link.id))
        return

    pageTotal = page.locator(".pagination.pagination-mini").locator(".page-info").text_content()
    pageNum = int(pageTotal.split("/")[1])
    logger.info("url {} pageTotal {} pagenum: {} ".format(url,pageTotal,pageNum))

    tempCurrentPage = int(pageTotal.split("/")[0])
    if currentPage > tempCurrentPage:
        page.locator('.pagination input[name="pageNo"]').fill(str(currentPage))
        page.locator('.pagination button').click()

    page.wait_for_timeout(1000)
    passCheck(page)

    goodSum = page.locator('xpath=//*[@id="shop-search-list"]/div/div[2]/span').text_content().replace(" ", "")
    monLinkBatch = MonLinkBatch(linkId=linkId)
    linkBatchCount = dbmapper.MonLinkBatchMapper.count(monLinkBatch)
    if int(linkBatchCount) > 0:
        logger.info("更新。。。")
        dbmapper.MonLinkBatchMapper.update(MonLinkBatch(goodSum=goodSum, linkId=linkId))
    else:
        monLinkBatchInsert = MonLinkBatch(goodSum=goodSum, linkId=linkId, status='0', linkUrl=url)
        dbmapper.MonLinkBatchMapper.insert(monLinkBatchInsert)

    logger.info("第{}页".format(currentPage))

    #print("第"+str(p_num)+"页 url size: "+str(len(pageLinks)))
    pageLinks = page.locator(".item3line1").locator(".item").all()
    if len(pageLinks) == 0:
        pageLinks = page.locator(".item30line1").locator(".item").all()

    logger.info("当前页面商品链接集合大小：{}".format(len(pageLinks)))

    if len(pageLinks) > 0:
        view_page(currentPage, i, page, pageLinks, url, linkId)
    logger.info("结束遍历 {}".format(url))

    if currentPage == pageNum:
        monLinkUpdate = MonLink(version=version, id=linkId, currentPage=currentPage, totalPage=pageNum)
        dbmapper.MonLinkMapper.update_by_id(monLinkUpdate)
    else:
        monLinkUpdate = MonLink(version=(int(version) - 1), id=linkId, currentPage=currentPage, totalPage=pageNum)
        dbmapper.MonLinkMapper.update_by_id(monLinkUpdate)
    # Close page
    page.close()
    # ---------------------
    context.close()
    browser.close()


def view_page(p_num:int,i:int,page,pageLinks, linkUrl, linkId):
    for p in pageLinks:
        i = i + 1
        j = 0
        logger.info("网页爬取进度:{}/{}".format(i, len(pageLinks)))
        try:
            with page.expect_popup(timeout=60000) as popup_info:
                p.first.click()
                page.wait_for_timeout(1000)
            page1 = popup_info.value

            page1.wait_for_timeout(1000)
            passCheck(page1)

            pic = linkpath_prefix + str(p_num) + "-" + str(i) + "-" + str(j) + page1.title().replace("*", "").replace(
                "/", "#").replace(" ", "")[0:30] + ".png"
            page1.wait_for_load_state('load')

            # y = page1.evaluate("() => document.documentElement.scrollHeight")
            # page1.locator("#J_SiteFooter").scroll_into_view_if_needed()
            # while page1.locator('.tb-footer-bd').is_hidden():
            #     page1.mouse.wheel(0, 1000);

            passCheck(page1)
            page1.keyboard.down('End')

            page1.screenshot(path=pic,full_page=True)

            linkMonResult = LinkMonResult(str(uuid.uuid4().int), linkUrl, page1.url,
                                          "/profile" + pic.replace(work_dir, ""), linkId=linkId)
            count = dbmapper.LinkMonResultMapper.count(linkMonResult)
            if int(count) > 0:
                linkMonResult.status = 0
                linkMonResult.ocrStr = ""
                dbmapper.LinkMonResultMapper.update(linkMonResult)
            else:
                logger.info("新增。。。")
                dbmapper.LinkMonResultMapper.insert(linkMonResult)

            linkVersion = MonLinkVersion(linkId=linkId, linkUrl=linkUrl, goodUrl=page1.url, version=version)
            lvCount = dbmapper.MonLinkVersionMapper.count(linkVersion);
            if int(lvCount) <= 0:
                logger.info("新增。。。")
                dbmapper.MonLinkVersionMapper.insert(linkVersion)

            page1.close()
        except UnboundLocalError as e:
            logger.exception("UnboundLocalError traceback is:{}".format(e))
        except InvalidStateError as e:
            logger.exception("InvalidStateError traceback is:{}".format(e))
        except TimeoutError as e:
            logger.exception("TimeoutError traceback is:{}".format(e))
        except Exception as e:
            logger.info("exception {} url:{}".format(i,page1.url))
            logger.exception("exception traceback is:{}".format(e))
            page1.close()

def passCheck(page):
    while page.locator(".captcha-tips").count() >0:
        logger.info("出现图形验证等待5秒...1")
        page.wait_for_timeout(5000)

    while page.locator("text=Sorry, we have detected unusual traffic from your network.").count() > 0:
        logger.info("出现图形验证等待5秒...2")
        page.wait_for_timeout(5000)

    while page.locator(".baxia-dialog-close").is_visible():
        logger.info("出现图形验证等待5秒...3")
        # page.locator(".baxia-dialog-close").click()
        page.wait_for_timeout(5000)

    while page.locator(".sufei-dialog-close").is_visible():
        logger.info("出现图形验证等待5秒...4")
        # page.locator(".sufei-dialog-close").click()
        page.wait_for_timeout(5000)

    while page.locator(".J_MIDDLEWARE_FRAME_WIDGET > img:nth-child(1)").is_visible():
        logger.info("出现图形验证等待5秒---5")
        # page.locator('.J_MIDDLEWARE_FRAME_WIDGET > img:nth-child(1)').click()
        page.wait_for_timeout(5000)


os.chdir(os.path.dirname(__file__))
# 通过db加载url
monLink = dbmapper.MonLinkMapper.list_by_typea('tb', version)
if len(monLink) > 0:
    monLinks = monLink

logger.info("待监测的url %s",[(link.id,link.url) for link in monLinks])
currentI = 0
if len(monLinks) > 0:
    for link in monLinks:
        currentI = currentI + 1
        logger.info("整体进度：{}/{}".format(currentI, len(monLinks)))
        with sync_playwright() as playwright:
            run(playwright, link)